package Test;

import DB.*;
import Model.Attendance;
import Model.Course;
import Model.Session;
import Model.Student;
import org.junit.Assert;
import org.junit.Test;
import Ctr.AttendanceCtr;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static org.junit.Assert.fail;

public class AttendanceCtrTest {
    //OK
    @Test
    public void testGetCourseAttendanceByTeacherIDAndCurrentDate()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        TeacherDB teacherDB = new TeacherDB();
        ArrayList<Attendance> attendances = attendanceCtr.getCourseAttendanceByTeacherIDAndCurrentDate(teacherDB.findTeacherByID(23),"2013-09-02");

        if(attendances != null)
        {
            for(Attendance attendance : attendances)
            {
                System.out.println(attendance.getId());
                System.out.println(attendance.getComment());
                System.out.println(attendance.getIsAttending());
                System.out.println(attendance.getStudentId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getSessionId().getId());
                System.out.println(attendance.getSessionId().getTeacherId().getName());
                System.out.println(attendance.getSessionId().getTeacherId().getInitials());
                System.out.println(attendance.getSessionId().getCourseId().getName());
                System.out.println(attendance.getSessionId().getDate());
            }
        }
        else
        {
            fail("Could not find any attendances");
        }

    }
    //OK
    @Test
    public void testGetAllAttendanceForOneStudentByIDAndDate()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        StudentDB studentDB = new StudentDB();

        String result = attendanceCtr.getAllAttendanceForOneStudentByIDAndDate(studentDB.findStudentByID(1),"2013-12-20");

        if(result != null)
        {
            System.out.println(result);
        }
        else
        {
            fail("Could not find any attendance amount");
        }
    }
    //OK
    @Test
    public void testGetAllCoursesForOneStudent()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        StudentDB studentDB = new StudentDB();
        ArrayList<Course> courses = attendanceCtr.getAllCoursesForOneStudent(studentDB.findStudentByID(1));

       if(courses != null)
       {
           for (Course c : courses) {
               System.out.println(c.getName());
           }
       }
       else
       {
           fail("Could not find any courses");
       }

    }

    //OKay
    @Test
    public void testGetSessionByTeacherAndDate()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        TeacherDB teacherDB = new TeacherDB();

        Session session = attendanceCtr.getSessionByTeacherAndDate(teacherDB.findTeacherByID(23),"2013-09-02");
        if(session != null)
        {
            System.out.println(session.getId());
            System.out.println(session.getDate());
            System.out.println(session.getTeacherId().getId());
            System.out.println(session.getTeacherId().getInitials());
            System.out.println(session.getTeamId().getId());
            System.out.println(session.getTeamId().getName());
            System.out.println(session.getCourseId().getId());
            System.out.println(session.getCourseId().getName());
        }
        else
        {
            fail("Failed finding session - session is null");
        }

    }

    //OKay
    @Test
    public void testGetCourseAttendanceByIDAndDate()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        StudentDB studentDB = new StudentDB();
        String result = attendanceCtr.getCourseAttendanceByIDAndDate(studentDB.findStudentByID(1),1,"2013-09-02");

        if(result != null)
        {
            System.out.println(result);
        }
        else
        {
            fail("Could not find any attendance amount");
        }

    }
    //okay
    @Test
    public void testStudentCheckIn()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        StudentDB studentDB = new StudentDB();

        boolean result = attendanceCtr.studentCheckIn(studentDB.findStudentByID(1),"2013-09-02");

        if(result)
        {
            System.out.println("Student was checked in");
        }
        else
        {
            fail("Error in student check in");
        }
    }
    //ok
    @Test
    public void testChangeAttendance()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        AttendanceDB attendanceDB = new AttendanceDB();
        Attendance attendance = attendanceDB.findAttendance(1);
        attendanceCtr.changeAttendance(1022442,attendance.getSessionId());
        Attendance updatedAttendance = attendanceDB.findAttendance(1);

        if(attendance.getIsAttending() != updatedAttendance.getIsAttending())
        {
            System.out.println("Attendance change was successful");
        }
        else
        {
            fail("Attendance change not successful");
        }

    }
    //OK
    @Test
    public void testChangeComment()
    {
        String comment = "UPDATE";

        AttendanceCtr attendanceCtr = new AttendanceCtr();
        AttendanceDB attendanceDB = new AttendanceDB();
        Attendance attendance = attendanceDB.findAttendance(1);
        attendanceCtr.changeComment(1022442, attendance.getSessionId(), comment);
        Attendance updatedAttendance = attendanceDB.findAttendance(1);

        if(!attendance.getComment().equals(updatedAttendance.getComment()))
        {
            System.out.println("Comment change was successful");
        }
        else
        {
            fail("Comment change not successful");
        }

    }
    //OK
    @Test
    public void testGetCurrentDate()
    {
        AttendanceCtr attendanceCtr = new AttendanceCtr();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String currentDate = dateFormat.format(date);
        String reslut = attendanceCtr.getCurrentDate();

        if(currentDate.equals(reslut))
        {
            System.out.println("The current date is correct");
        }
        else
        {
            fail("The current date is not correct");
        }

    }
}
